package com.topscomm.pm.quartz;

import com.topscomm.cbo.pojo.CboDeptEntity;
import com.topscomm.cbo.pojo.CboOrgEntity;
import com.topscomm.cbo.pojo.CboUserEntity;
import com.topscomm.cbo.service.impl.CboDeptService;
import com.topscomm.cbo.service.impl.CboOrgService;
import com.topscomm.cbo.service.impl.CboUserService;
import com.topscomm.pm.controller.PmCloseApplyController;
import com.topscomm.pm.pojo.PmCloseApplyEntity;
import com.topscomm.pm.pojo.PmProjectEntity;
import com.topscomm.pm.service.impl.PmCloseApplyService;
import com.topscomm.pm.service.impl.PmProjectService;
import com.topscomm.pub.util.DateUtil;
import com.topscomm.pub.util.SpringUtil;
import com.topscomm.pub.util.StringBufferProxy;
import com.topscomm.pub.vo.ResponseResult;
import com.topscomm.tap.TapEntity;
import com.topscomm.tap.common.FlowDatacodeCache;
import com.topscomm.tap.common.SessionUserBean;
import com.topscomm.tap.quartz.TapJob;
import org.quartz.JobExecutionContext;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author: shenyouyi
 * @date: 2020/5/25
 * @description:
 */
@Component
public class PmProjectCloseApplyJob extends TapJob {

    @Resource
    private ApplicationContext applicationContext;

    @Override
    public void run(JobExecutionContext jobExecutionContext) {
        // 查询出完工状态的，且没有闭环申请的项目
        StringBufferProxy sql = new StringBufferProxy();
//        sql.appendSingle("projectstate = '3' and not exists (");
//        modify by wushenghong  补充项目闭环条件
        sql.appendSingle(" projectstate = '3'");
        sql.appendSingle(" and projectmoney = recbillmoney");
        sql.appendSingle(" and projectmoney = billmoney");
        sql.appendSingle(" and projectmoney = contractmoney and not exists (");
        sql.appendSingle("select pmcloseapply.id from  pmcloseapply where pmcloseapply.state in ('0','1','2') and pmcloseapply.projectid = pmproject.id)");
        PmProjectService pmProjectService = (PmProjectService) applicationContext.getBean("pmProjectService");
        PmCloseApplyController pmCloseApplyController = (PmCloseApplyController) applicationContext
            .getBean("pmCloseApplyController");
        List<PmProjectEntity> projectList = pmProjectService.queryByWhere(sql.toString());
        for (PmProjectEntity project : projectList) {
            try {
                pmCloseApplyController.saveAndSubmitProjectCloseApply(project);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }


}
